#include "arena_1dfood.h"
#include <iostream>

using namespace std;

Arena_1dfood::Arena_1dfood()
{
  reset();

  objects = new Objects(1000, 1000);
  Line l(0, 900, 1000, 900);
  objects->add(l);
  Circle c(850, 880, 20);
  objects->add(c);
  Rectangle r(50, 800, 100, 100);
  objects->add(r);

  update(0.0);
}

Arena_1dfood::~Arena_1dfood()
{
}

void Arena_1dfood::reset(void)
{
  _distance_to_food = 10;
  _speed = 0;
  Arena_base::reset();
}

float Arena_1dfood::get_distance_to_food(void)
{
  return _distance_to_food;
}

void Arena_1dfood::set_speed(float speed)
{
//  cout << " # Arena: Setting speed to " << speed << endl;
  _speed = speed;
}

boost::function<void(float)> Arena_1dfood::set_speed_function(void)
{
  return std::bind1st(std::mem_fun(&Arena_1dfood::set_speed), this);
}

void Arena_1dfood::eat(float volume)
{
  if (_distance_to_food > 1) {
    cout << "  COULD NOT EAT: Distance too large: " << _distance_to_food
        << endl;
    return;
  }
  volume = 0;
//  cout << " # Arena: Eating" << endl;
}

boost::function<void(float)> Arena_1dfood::eat_function(void)
{
  return std::bind1st(std::mem_fun(&Arena_1dfood::eat), this);
}

void Arena_1dfood::update(float time)
{
  if (time < _time) {
    cout << "ERROR: delta time negative" << endl;
    exit(1);
  }
  _distance_to_food -= _speed * (time - _time);
  //  cout << " # Arena: Distance to food: " << _speed << " " << _distance_to_food
  //      << endl;
  _time = time;
  print_state_to_file();
  _speed = 0.0;

  update_arena();
}

float *Arena_1dfood::get_distance_to_food_p(void)
{
  return &_distance_to_food;
}

std::string Arena_1dfood::get_state(void)
{
  char s[400];

  sprintf(s, "%5.2f: d=%8.3f, v=%8.3f", _time, _distance_to_food, _speed);

  return string(s);
}

void Arena_1dfood::update_arena(void)
{
  objects->rectangles[0].p1.x = 850 - 700 * _distance_to_food / 10.0 - 100/2.0;
}
